package com.chang.rentmall.servicesImpl;

import com.chang.rentmall.beans.*;
import com.chang.rentmall.daos.AddressDao;
import com.chang.rentmall.daos.GoodsDao;
import com.chang.rentmall.daos.OrderDao;
import com.chang.rentmall.daos.OrderGoodsDao;
import com.chang.rentmall.enums.ORDER_STATUS;
import com.chang.rentmall.forms.OrderFormBean;
import com.chang.rentmall.services.OrderService;
import com.chang.rentmall.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    OrderDao orderDao;
    @Autowired
    AddressDao addressDao;
    @Autowired
    GoodsDao goodsDao;
    @Autowired
    OrderGoodsDao orderGoodsDao;
    @Override
    public MessageBean addOrder(OrderBean orderBean) {
        MessageBean messageBean = new MessageBean();
        String nowTime = Utils.getLocalDateTime();
        orderBean.setCreateTime(nowTime);
        orderBean.setUpdateTime(nowTime);
        orderBean.setStatus(ORDER_STATUS.NOT_PAY.getIndex()); // 订单状态设置成待支付
        // 创建订单号
        orderBean.setOrderNumber(Utils.getUUID());
        // 根据ids获取商品信息
        List<GoodsBean> goodsList = goodsDao.getGoodsByIds(orderBean.getGoodsIds());
        for (GoodsBean goodsBean : goodsList) {
            OrderGoodsBean orderGoodsBean = new OrderGoodsBean();
            orderGoodsBean.setPrice(goodsBean.getPrice());
            orderGoodsBean.setDeposit(goodsBean.getDeposit());
            orderGoodsBean.setGoodsId(goodsBean.getId());
            orderGoodsBean.setShopId(goodsBean.getShopId());
            orderGoodsBean.setGoodsName(goodsBean.getName());
            orderGoodsBean.setPics(goodsBean.getPicUrls());
            orderGoodsBean.setOrderId(orderBean.getOrderNumber());

            int flag = orderGoodsDao.addOrderGoods(orderGoodsBean);
            logger.info("插入订单： " + orderGoodsBean.getOrderId() + "  -  商品： " + orderGoodsBean.getGoodsId());
        }
        int flag = orderDao.addOrder(orderBean);
        if (flag == 1) {
            logger.info("成功创建订单： " + orderBean.getOrderNumber());
            messageBean.setValues(200, "创建订单成功", orderBean);
        } else {
            logger.info("创建订单： " + orderBean.getOrderNumber() + "失败");
            messageBean.setValues(-1, "创建订单失败", "");
        }
        return messageBean;
    }

    @Override
    public MessageBean updateOrder(OrderBean orderBean) {
        MessageBean messageBean = new MessageBean();
        orderBean.setUpdateTime(Utils.getLocalDateTime());
        int flag = orderDao.updateOrder(orderBean);
        if (flag == 1) {
            logger.info("修改订单: " + orderBean.getOrderNumber() + "成功");
            messageBean.setValues(200, "操作成功", "");
        } else {
            logger.info("修改订单: " + orderBean.getOrderNumber() + "失败");
            messageBean.setValues(-1, "操作失败", "");
        }
        return messageBean;
    }

    @Override
    public MessageBean getOrdersByShop(OrderFormBean orderFormBean) {

        return null;
    }

    /**
     * 查询某个店铺下某个客户的订单
     * @param orderFormBean
     * @return
     */
    @Override
    public MessageBean getShopOrdersByUser(OrderFormBean orderFormBean) {
        MessageBean messageBean = new MessageBean();
        List<OrderBean> list = orderDao.getShopOrdersByUser(orderFormBean.getQuery(), orderFormBean.getPagination());
        messageBean.setValues(200, "查询成功", list);
        logger.info("查询用户"+ orderFormBean.getQuery().getUserId() +"在店铺 "+ orderFormBean.getQuery().getId() +" 订单成功 ");
        return messageBean;
    }

    @Override
    public MessageBean getOrderByNumber(OrderFormBean orderFormBean) {
        MessageBean messageBean = new MessageBean();
        // 获取订单信息
        OrderBean orderBean = orderDao.getOrderByNumber(orderFormBean.getQuery().getOrderNumber());
        if (orderBean == null) {
            messageBean.setValues(-1, "此订单不存在", "");
        } else {
            // 获取订单商品列表
            List<OrderGoodsBean> orderGoodsBeanList = orderGoodsDao.getOrderGoodsByOrderId(orderBean.getOrderNumber());
            // 如果需要配送，获取地址
            if (orderBean.getAddressId() != 0) {
                AddressBean addressBean = addressDao.getAddressDetail(orderBean.getAddressId());
                orderBean.setAddress(addressBean);
            }
            orderBean.setGoodsArr(orderGoodsBeanList);
            messageBean.setValues(200, "获取数据成功", orderBean);
        }
        return messageBean;
    }

    @Override
    public MessageBean getOrdersByUser(OrderFormBean orderFormBean) {
        MessageBean messageBean = new MessageBean();
        List<OrderBean> list = orderDao.getOrdersByUser(orderFormBean.getQuery(), orderFormBean.getPagination());
        messageBean.setValues(200, "查询成功", list);
        logger.info("查询用户"+ orderFormBean.getQuery().getUserId() + " 的订单成功 ");
        return messageBean;
    }

    @Override
    public MessageBean getCustomersByShop(OrderFormBean orderFormBean) {
        MessageBean messageBean = new MessageBean();
        List<CustomerBean> list = orderDao.getCustomersByShop(orderFormBean.getQuery(), orderFormBean.getPagination());
        messageBean.setValues(200, "查询成功", list);
        return messageBean;
    }

    @Override
    public MessageBean deleteOrder(OrderFormBean orderFormBean) {
        MessageBean messageBean = new MessageBean();
        String orderNumber = orderFormBean.getQuery().getOrderNumber();
        int flag = orderDao.deleteOrder(orderFormBean.getQuery().getOrderNumber());
        if (flag == 1) {
            logger.info("删除订单: " + orderFormBean.getQuery().getOrderNumber() + "成功");
            messageBean.setValues(200, "删除成功", "");
        } else {
            logger.info("删除订单: " + orderNumber + "失败");
            messageBean.setValues(-1, "删除失败", "");
        }
        return messageBean;
    }
}
